<template>
  <section>
    <van-sticky>
      <PageHeader title="营销" />
    </van-sticky>

    <van-tabs v-model="activeTab" sticky @click="onTabChange">
      <van-tab title="会员卡" name="card">
        <div class="pd10">
          <van-button type="primary" round block @click="$router.push({name: 'MarketingCreateVipCard'})">+创建会员卡</van-button>
        </div>

        <van-list
          class="mt10"
          v-model="loading"
          :finished="finished"
          finished-text="没有更多了"
          @load="requestList">
          <div class="flex flex-col bg-color-white mb10 font-common"
               v-for="(item, index) in dataList"
               :key="index">
            <div class="flex flex-row flex-left pd10 font-big strong">{{item.name}}</div>
            <div class="flex flex-row flex-left pd10">会员等级：{{item.vip.name}}</div>
            <div class="flex flex-row flex-left pd10" v-html="item.vip.rights.desc"></div>
            <div class="flex flex-row flex-left pd10 color-gray">{{item.desc}}</div>
            <div class="flex flex-row flex-left pd10">状态：<label v-if="item.enable==1">上架</label><label v-else-if="item.enable==0">下架</label></div>
            <div class="flex flex-row flex-center font-common border-top-gray pt10 pb10">
              <div class="flex flex-row flex-center flex-1" @click="$router.push({name: 'MarketingEditVipCard', params: {id: item.id}})">
                <i class="iconfont icon-bianji mr5" />编辑
              </div>
              <div class="flex flex-row flex-center flex-1" @click="offline(index)" v-if="item.enable==1">
                <i class="iconfont icon-shangxianchanpin- mr5" />下线
              </div>
              <div class="flex flex-row flex-center flex-1" @click="online(index)" v-else-if="item.enable==0">
                <i class="iconfont icon-shangxian mr5" />上线
              </div>
              <div class="flex flex-row flex-center flex-1" @click="$router.push({name: 'MarketingVipCardNo', params: {vip_card_id: item.id}})">
                <i class="iconfont icon-baoming mr5" />卡号管理
              </div>
            </div>
          </div>
        </van-list>
      </van-tab>

      <van-tab title="活动" name="activity">

      </van-tab>
    </van-tabs>
  </section>
</template>

<script>
import PageHeader from "../componets/PageHeader";
import {vipCardListsUri, vipCardOfflineUri, vipCardOnlineUri} from "../../common/api";

export default {
  components: {
    PageHeader
  },
  data() {
    return {
      activeTab: 'card',
      currentPage: 1,
      lastPage: 0,
      total: 0,
      pageSize: 10,
      loading: false,
      finished: false,
      dataList: [],
    }
  },
  methods: {
    onTabChange(name) {
      this.activeTab = name;
      if(name == 'activity') {
        this.$router.replace({name: 'MarketingActivityLists'});
      }
    },
    online(index) {
      let item = this.dataList[index];
      const id = item.id;
      const that = this;
      this.$http.get(vipCardOnlineUri + '?id=' + id).then(response => {
        let {code, msg, data} = response.body
        if (code != 0) {
          that.toast.fail(msg);
        } else {
          item.enable = item.enable == 1 ? 0 : 1;
          that.$set(that.dataList, index, item);
        }
      }, response => {
        // error callback
      })
    },
    offline(index) {
      let item = this.dataList[index];
      const id = item.id;
      const that = this;
      this.$http.get(vipCardOfflineUri + '?id=' + id).then(response => {
        let {code, msg, data} = response.body
        if (code != 0) {
          that.toast.fail(msg);
        } else {
          item.enable = item.enable == 1 ? 0 : 1;
          that.$set(that.dataList, index, item);
        }
      }, response => {
        // error callback
      })
    },
    requestList() {
      const that = this;
      this.loading = true;
      let url = vipCardListsUri + '?page_size=' + that.pageSize + '&page=' + that.currentPage;
      this.$http.get(url).then(response => {
        let {code, msg, data} = response.body
        if (code != 0) {
          that.toast.fail(msg);
        } else {
          that.loading = false;
          if (that.currentPage >= data.last_page) {
            that.finished = true;
          }
          that.currentPage = that.currentPage + 1;
          for (let i = 0; i < data.data.length; i++) {
            that.dataList.push(data.data[i]);
          }
          that.total = data.total;
          that.lastPage = data.last_page;
        }
      }, response => {
        // error callback
      })
    },
  },
  mounted: function () {

  }
}
</script>

<style scoped>

</style>
